import Layout from '@/layout/index.vue'
import type { MenuProps, ItemType } from 'ant-design-vue'
import {
  PieChartOutlined,
  HomeOutlined,
  UserOutlined,
  TeamOutlined,
  FileOutlined,
  UsergroupAddOutlined,
  ReadOutlined,
  TagsOutlined,
  SettingOutlined,
  ApartmentOutlined,
  SecurityScanOutlined,
  DashboardOutlined,
} from '@ant-design/icons-vue'
import type { RouteRecordRaw } from 'vue-router'

/** 常驻路由 除了 redirect/403/404/login 等隐藏页面，其它页面建议设置 Name 属性 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login/index.vue'),
    meta: { title: '登录', hidden: true },
  },

  {
    path: '/redirect',
    name: 'Redirect',
    component: Layout,
    meta: { title: '路由重定向', hidden: true },
    children: [{ path: '/redirect/:path(.*)', component: () => import('@/views/Redirect/index.vue') }],
  },
  {
    path: '/',
    name: 'Layout',
    component: Layout,
    redirect: '/home',
    children: [
      // {
      //   path: '/dashboard',
      //   name: 'Dashboard',
      //   component: () => import(`@/views/Dashboard/index.vue`),
      //   meta: { isMenu: true, title: '系统总览', menuIcon: DashboardOutlined },
      // },
      {
        path: '/home',
        name: 'Home',
        component: () => import(`@/views/Home/index.vue`),
        meta: { isMenu: true, title: '首页', menuIcon: HomeOutlined },
      },
      {
        path: '/article',
        name: 'Article',
        component: () => import(`@/views/Article/index.vue`),
        meta: { isMenu: true, title: '文章管理', menuIcon: ReadOutlined },
        children: [
          {
            path: '/article/add',
            name: 'ArticleAdd',
            component: () => import('@/views/Article/add/index.vue'),
            meta: { title: '新增文章', isMenu: false },
          },
          {
            path: '/article/list',
            name: 'ArticleList',
            component: () => import('@/views/Article/list/index.vue'),
            meta: { title: '文章列表', isMenu: true, menuIcon: ReadOutlined },
          },
        ],
      },
      {
        path: '/tag',
        name: 'Tag',
        component: () => import(`@/views/Tag/index.vue`),
        meta: { isMenu: true, title: '标签管理', menuIcon: TagsOutlined },
      },
      {
        path: '/category',
        name: 'Category',
        component: () => import(`@/views/Category/index.vue`),
        meta: { isMenu: true, title: '分类管理', menuIcon: ApartmentOutlined },
      },
      {
        path: '/system',
        name: 'System',
        redirect: '/system/user',
        component: () => import(`@/views/System/index.vue`),
        meta: { isMenu: true, title: '系统管理', menuIcon: SettingOutlined },
        children: [
          {
            path: '/system/user',
            name: 'User',
            component: () => import(`@/views/User/index.vue`),
            meta: { isMenu: true, title: '用户中心', menuIcon: UsergroupAddOutlined },
          },
          {
            path: '/system/role',
            name: 'Role',
            component: () => import(`@/views/Role/index.vue`),
            meta: { isMenu: true, title: '角色管理', menuIcon: TeamOutlined },
          },
          {
            path: '/system/permission',
            name: 'Permission',
            component: () => import(`@/views/Permission/index.vue`),
            meta: { isMenu: true, title: '权限管理', menuIcon: SecurityScanOutlined },
          },
        ],
      },
      {
        path: '/file',
        name: 'File',
        component: () => import(`@/views/File/index.vue`),
        meta: { isMenu: true, title: '文件管理', menuIcon: FileOutlined },
      },
    ],
  },

  // The not found page must be placed last
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/ExceptionPage/NotFound.vue'),
    meta: { title: 'NotFound', hidden: true },
    alias: '/:pathMatch(.*)*',
  },
]

function getItem(label: string, key: string, icon?: any, children?: ItemType[], type?: 'group'): ItemType {
  const item = {
    key,
    icon,
    label,
    type,
    children,
  }
  if (!children || !children.length) {
    delete item.children
  }
  return item
}

// 菜单
export function getMenuItems(list = constantRoutes.find((v) => v.path === '/')?.children || []): RouteRecordRaw[] {
  if (!list || !list.length) return []
  return list
    .filter((v) => v.meta?.isMenu)
    .map((e) => {
      return {
        path: e.path,
        name: e.name,
        meta: e.meta,
        children: getMenuItems(e.children || []),
      }
    })
}
